package com.huike.game.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.huike.game.R;

public class Game10ImageDesc extends AppCompatActivity {

    private ImageView mIvGame10ImageDesc;
    private RequestQueue queue;
    private LinearLayout ll_game10_imageDesc;
    @SuppressLint("HandlerLeak")
    private Handler  handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if(msg.what == 1){
                isBack = false;
            }

        }
    };

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game10_image_desc);

        // 隐藏状态栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 显示状态栏
//        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        Intent intent = getIntent();
//        int intExtra = intent.getIntExtra("image",0);
        int image = intent.getIntExtra("image",999);
        mIvGame10ImageDesc = findViewById(R.id.iv_game10_image_desc);
        ll_game10_imageDesc = findViewById(R.id.ll_game10_imageDesc);

//        网络请求获取图片
        queue = Volley.newRequestQueue(this);

        if(image == 999){
            ImageRequest request = new ImageRequest("http://dmimg.5054399.com/allimg/pkm/pk/22.jpg", new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap bitmap) {
                    mIvGame10ImageDesc.setImageBitmap(bitmap);
                    Log.e("TAG","加载成功");
                }
            }, 500, 500, ImageView.ScaleType.CENTER, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Log.e("TAG",volleyError.getMessage());
                    Log.e("TAG","加载失败");

                }
            });

            queue.add(request);
        }

      mIvGame10ImageDesc.setImageResource(image);

        ll_game10_imageDesc.setOnTouchListener(new View.OnTouchListener() {
            float currentDistance;
            float lastDistance = -1;
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                if(motionEvent.getAction() == MotionEvent.ACTION_MOVE){

                        if(motionEvent.getPointerCount() >=2 ){
                            float offsetX = motionEvent.getX(0) - motionEvent.getX(1);
                            float offsetY = motionEvent.getY(0) - motionEvent.getY(1);

                            currentDistance = (float) Math.sqrt(offsetX*offsetX + offsetY*offsetY);

                            if(currentDistance < 0){
                                lastDistance = currentDistance;
                            }else{
                                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mIvGame10ImageDesc.getLayoutParams();
                                if(currentDistance -lastDistance > 5){
                                    params.width = (int) (1.1f * mIvGame10ImageDesc.getWidth());
                                    params.height = (int) (1.1f * mIvGame10ImageDesc.getHeight());
                                    mIvGame10ImageDesc.setLayoutParams(params);
                                    lastDistance = currentDistance;

                                } else if(lastDistance - currentDistance > 5){

                                    params.width = (int) (0.9f * mIvGame10ImageDesc.getWidth());
                                    params.height = (int) (0.9f * mIvGame10ImageDesc.getHeight());
                                    mIvGame10ImageDesc.setLayoutParams(params);
                                    lastDistance = currentDistance;
                                    }
                            }
                        }
                }

                return true;
            }
        });


    }



    private Boolean isBack = false; //默认不返回
//    监听手机按键 点击2次返回页面
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK ){ //监听返回按钮

            if(!isBack){
                isBack = true;
                Toast.makeText(this, "在按一次返回", Toast.LENGTH_SHORT).show();
                handler.sendEmptyMessageDelayed(1,2000);
                return true; //消费
            }

        }
        return super.onKeyUp(keyCode, event);

    }

    //长安返回按钮监听
    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {

        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
//            finish();
        }

        return super.onKeyLongPress(keyCode, event);
    }


}
